package com.example.com.myapplication.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.example.com.myapplication.IntentDataHelper;
import com.example.com.myapplication.R;
import com.megvii.facetrack.FaceTrackListener;
import com.megvii.facetrack.FaceTrackOption;
import com.megvii.facetrack.FaceTracker;
import com.megvii.facetrack.MVFace;
import com.megvii.facetrack.camera.MVCameraPreview;

/**
 * @author by licheng on 2017/7/5.
 */
public class MainActivity extends PermissionActivity implements FaceTrackListener, View.OnClickListener {
    public static final String OPTION = "option";

    private MVCameraPreview cameraPreview;
    private FaceTracker faceTracker;

    private FaceTrackOption option;

    private ImageView takePicture;
    private ImageView imageFace;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        cameraPreview = (MVCameraPreview) findViewById(R.id.camera_preview);
        takePicture = (ImageView) findViewById(R.id.image_take_picture);
        imageFace = (ImageView) findViewById(R.id.image_face);
        takePicture.setOnClickListener(this);
        faceTracker = new FaceTracker();
        option = getIntent().getParcelableExtra(OPTION);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isPermissionGranted) {
            startTrack();
        }
    }

    private void startTrack() {
        faceTracker.start(this, cameraPreview, option, this);
    }

    @Override
    public void onTrackCompleted(@NonNull MVFace face) {
        // 质量判断
        String errorMessage = face.getErrorMessage();
        cameraPreview.setErrorMessage(errorMessage);
        if (!TextUtils.isEmpty(errorMessage)) {
            takePicture.setVisibility(View.GONE);
            return;
        }
        // 有没有人脸
        if (null == face.getCropFace() || 0 == face.getCropFace().size()) {
            takePicture.setVisibility(View.GONE);
            return;
        }
        IntentDataHelper.setFaceList(face.getCropFace());
        IntentDataHelper.setBigFaceList(face.getOriginalFace());
        if (option.isEnableAutoStop()) {
            toResultActivity();
        } else if (option.isModeTakePicture()) {
            takePicture.setVisibility(View.VISIBLE);
        } else {
            if (View.VISIBLE == imageFace.getVisibility()) {
                return;
            }
            byte[] faceImage = face.getCropFace().get(0);
            imageFace.setVisibility(View.VISIBLE);
            imageFace.setImageBitmap(BitmapFactory.decodeByteArray(faceImage, 0, faceImage.length));
        }
    }

    private void toResultActivity() {
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    @Override
    public void onClick(View v) {
        if (R.id.image_take_picture == v.getId()) {
            faceTracker.takePicture();
            toResultActivity();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        faceTracker.stop();
    }

    @Override
    protected String[] getPermissionList() {
        return new String[]{Manifest.permission.CAMERA};
    }
}
